var fs = require("fs");
var path = require("path");

const fpath = path.join(__dirname, "./visitors.txt");
let visitors = fs.readFileSync(fpath, "utf-8");
visitors = Number(visitors);

function record(req, res, next){
    if(req.url == '/' || req.url == '/index'){
        visitors++;
    }
    next();
}

//每隔10小时将访问人数写入文件中
setInterval(() => {
    fs.writeFile(fpath, visitors, "utf-8", () => { console.log("写入成功"); });
}, 10 * 3600 * 1000);

exports.record = record;
